package com.ln.lnzw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.activity.ConsultingDepartmentActivity;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseFragment;
import com.ln.lnzw.bean.UserSuggestionBean;
import com.ln.lnzw.event.DepartmentConsultingEvent;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.view.ClearableEditText;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/5/9.
 * 我的咨询
 */

public class MyConsultFragment extends BaseFragment implements View.OnClickListener{
    @BindView(R.id.cet_zixunshixiang)
    ClearableEditText cetZixunshixiang;
    @BindView(R.id.cet_my_name)
    ClearableEditText cetMyName;
    @BindView(R.id.cet_phone)
    ClearableEditText cetPhone;
    @BindView(R.id.cet_address)
    ClearableEditText cetAddress;
    @BindView(R.id.cet_biaoti)
    ClearableEditText cetBiaoti;
    @BindView(R.id.cet_content)
    ClearableEditText cetContent;
    Unbinder unbinder;
    @BindView(R.id.spinner)
    ClearableEditText spinner;
    @BindView(R.id.cet_shenfenzhenghao)
    ClearableEditText cetShenfenzhenghao;
    @BindView(R.id.tv_fasong)
    TextView tvFasong;
    @BindView(R.id.tv_chongxie)
    TextView tvChongxie;
    @BindView(R.id.cet_youxiang)
    ClearableEditText cetYouxiang;
    private String groupCDesc;//部门标识
    private String itemCode;//事项编码
    private View layout;
    private TextView textView;
    private EditText editText;
    private AlertDialog dailog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myconsultfragment_activity, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        cetZixunshixiang.setFocusable(false);
        EventBus.getDefault().register(this);
        setListener();
        cetMyName.setFocusable(false);
        cetShenfenzhenghao.setFocusable(false);
        cetMyName.setText(activity.spUtils.getString(AppConstant.TRUE_NAME));
        cetShenfenzhenghao.setText(activity.spUtils.getString(AppConstant.USER_CREDITID));
        cetPhone.setText(activity.spUtils.getString(AppConstant.MOBILE));
        cetYouxiang.setText(activity.spUtils.getString(AppConstant.EMAIL));
    }

    private void setListener() {
        cetPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText()) || TextUtils.isEmpty(cetBiaoti.getText()) || TextUtils.isEmpty(cetContent.getText())||TextUtils.isEmpty(groupCDesc)||TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText()) && !TextUtils.isEmpty(cetBiaoti.getText()) && !TextUtils.isEmpty(cetContent.getText())&& !TextUtils.isEmpty(groupCDesc)&& !TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetYouxiang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText()) || TextUtils.isEmpty(cetBiaoti.getText()) || TextUtils.isEmpty(cetContent.getText())||TextUtils.isEmpty(groupCDesc)||TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText()) && !TextUtils.isEmpty(cetBiaoti.getText()) && !TextUtils.isEmpty(cetContent.getText())&& !TextUtils.isEmpty(groupCDesc)&& !TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText()) || TextUtils.isEmpty(cetBiaoti.getText()) || TextUtils.isEmpty(cetContent.getText())||TextUtils.isEmpty(groupCDesc)||TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText()) && !TextUtils.isEmpty(cetBiaoti.getText()) && !TextUtils.isEmpty(cetContent.getText())&& !TextUtils.isEmpty(groupCDesc)&& !TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetBiaoti.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText()) || TextUtils.isEmpty(cetBiaoti.getText()) || TextUtils.isEmpty(cetContent.getText())||TextUtils.isEmpty(groupCDesc)||TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText()) && !TextUtils.isEmpty(cetBiaoti.getText()) && !TextUtils.isEmpty(cetContent.getText())&& !TextUtils.isEmpty(groupCDesc)&& !TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText()) || TextUtils.isEmpty(cetBiaoti.getText()) || TextUtils.isEmpty(cetContent.getText())||TextUtils.isEmpty(groupCDesc)||TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText()) && !TextUtils.isEmpty(cetBiaoti.getText()) && !TextUtils.isEmpty(cetContent.getText())&& !TextUtils.isEmpty(groupCDesc)&& !TextUtils.isEmpty(itemCode)) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Subscribe
    public void onEvent(DepartmentConsultingEvent departmentConsultingEvent) {
        spinner.setText(departmentConsultingEvent.getGroupName());
        cetZixunshixiang.setText(departmentConsultingEvent.getName());
        itemCode=departmentConsultingEvent.getItemCode();
        groupCDesc=departmentConsultingEvent.getGroupCDesc();
        if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText()) || TextUtils.isEmpty(cetBiaoti.getText()) || TextUtils.isEmpty(cetContent.getText())||TextUtils.isEmpty(groupCDesc)||TextUtils.isEmpty(itemCode)) {
            tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
        }
        if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText()) && !TextUtils.isEmpty(cetBiaoti.getText()) && !TextUtils.isEmpty(cetContent.getText())&& !TextUtils.isEmpty(groupCDesc)&& !TextUtils.isEmpty(itemCode)) {
            tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
        }
    }
    @OnClick({R.id.spinner,R.id.tv_fasong, R.id.tv_chongxie})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.spinner:
                ActivityUtils.startActivity(activity, ConsultingDepartmentActivity.class);
                break;
            case R.id.tv_fasong:
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText()) || TextUtils.isEmpty(cetBiaoti.getText()) || TextUtils.isEmpty(cetContent.getText())||TextUtils.isEmpty(groupCDesc)||TextUtils.isEmpty(itemCode)) {
                   return;
                }
                //发送
                initData();
                break;
            case R.id.tv_chongxie:
                spinner.setText("");
                groupCDesc="";
                itemCode="";
                cetZixunshixiang.setText("");
                cetPhone.setText("");
                cetYouxiang.setText("");
                cetAddress.setText("");
                cetBiaoti.setText("");
                cetContent.setText("");
                break;
        }
    }

    private void initData() {
        HttpMethods.getInstanceCenter().info.getUserConsultation(activity.spUtils.getString(AppConstant.USER_UID),cetMyName.getText().toString(),cetShenfenzhenghao.getText().toString(),cetPhone.getText().toString(),cetYouxiang.getText().toString(),cetAddress.getText().toString(),groupCDesc,spinner.getText().toString(),itemCode,cetZixunshixiang.getText().toString(),cetBiaoti.getText().toString(),cetContent.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserSuggestionBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserSuggestionBean userSuggestionBean) {
                        Log.i("555", "onNext: "+userSuggestionBean.toString());
//                        userSuggestionBean.getResult().getApplySearchNumber(); 要弹框显示的数据
                        if ("200".equals(userSuggestionBean.getCode())){
                            ToastFactory.getToast(activity,userSuggestionBean.getMsg()).show();
                            getDialog("请记住以下查询编码和密码：",userSuggestionBean.getResult().getApplySearchNumber());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    private void getDialog(String tittle,String id) {
        LayoutInflater inflater = getLayoutInflater();
        layout = inflater.inflate(R.layout.user_dialog,
                (ViewGroup) activity.findViewById(R.id.rl_cardview));
        layout.findViewById(R.id.queding).setOnClickListener(this);
        textView = layout.findViewById(R.id.title);
        textView.setText(tittle);
        editText = layout.findViewById(R.id.et_content);
        editText.setText(id);
        dailog = new AlertDialog.Builder(activity)
                .setView(layout)
                .setCancelable(false)
                .show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.queding:
                dailog.dismiss();
                break;
        }
    }
}
